package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Mail;
import org.kjtc.entity.MailHistory;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.MailHistoryService;
import org.kjtc.service.MailServerMaintenanceService;
import org.kjtc.service.MailTemplateService;
import org.kjtc.util.MailSenderInfo;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MailController extends BaseController {

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@Resource(name = "mailTemplateService")
	private MailTemplateService mailTemplateService;

	@Resource(name = "mailServerMaintenanceService")
	private MailServerMaintenanceService mailServerMaintenanceService;

	@Resource(name = "mailHistoryService")
	private MailHistoryService mailHistoryService;

	@RequestMapping(value = "/searchMailTemplate")
	@ResponseBody
	public Map<String, Object> searchMailTemplate(Mail mailTemplate, String page, String rows) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息查询开始");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		List<Mail> list = null;
		try {
			list = mailTemplateService.getMailTemplatePage(mailTemplate, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt", pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("mailTemplateList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "模板管理信息查询");
		mailTemplate = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value="/saveMailTemplate")
	public Map<String, Object> saveMailTemplate(Mail mailTemplate){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息添加开始");
		int count = 0;
		try {
			mailTemplate.setCreateUser(this.getUesr().getUserID());
			mailTemplate.setUpdateUser(this.getUesr().getUserID());
			count = mailTemplateService.saveMailTemplate(mailTemplate);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())) {
				map.put("result", "MailIDNoNull");
			} else if("MailType".equals(error[3].trim())){
				map.put("result", "MailTypeNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息添加异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "模板管理信息添加");
		mailTemplate = null;
		return map;
	}

	@RequestMapping(value = "/updateMailTemplate")
	@ResponseBody
	public Object updateMailTemplate(Mail mailTemplate) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息修改开始");
		int count = 0;
		try {
			mailTemplate.setUpdateUser(this.getUesr().getUserID());
			count = mailTemplateService.updateMailTemplate(mailTemplate);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if("MailType".equals(error[3].trim())){
				map.put("result", "MailTypeNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息修改异常:" + e.getMessage());
		}
		mailTemplate = null;
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "模板管理信息修改");
		mailTemplate = null;
		return map;
	}

	@RequestMapping(value = "/deleteMailTemplate")
	@ResponseBody
	public Object deleteMailTemplate(String mailIDList) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息删除开始");
		Mail mailTemplate = new Mail();
		mailTemplate.setMailIDList(mailIDList.split(","));
		int count = 0;
		try {

			count = mailTemplateService.deleteMailTemplate(mailTemplate);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "模板管理信息删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "模板管理信息删除");
		mailTemplate = null;
		return map;
	}

	@RequestMapping(value = "/searchMailServerMaintenance")
	@ResponseBody
	public MailSenderInfo searchMailServerMaintenance() throws Exception {

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "服务器维护查询开始");
		// 获取所有设备数据
		MailSenderInfo mailSenderInfo = mailServerMaintenanceService.getMailServer();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "服务器维护查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "服务器维护查询");
		return mailSenderInfo;
	}

	@RequestMapping(value = "/updateMailServerMaintenance")
	@ResponseBody
	public Object updateMailServerMaintenance(MailSenderInfo mailSenderInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "服务器维护修改开始");
		try {
			boolean bool = mailServerMaintenanceService.updateMailServer(mailSenderInfo);
			if (bool) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "服务器维护添加异常:" + e.getMessage());
		}
		mailSenderInfo = null;
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "服务器维护修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "服务器维护修改");
		return map;
	}

	@RequestMapping(value = "/getMailHistory")
	@ResponseBody
	public Map<String, Object> getMailHistory(MailHistory info, String page, String rows) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "邮件日志查询开始");
		try {
			Page pageQuery = getPage();

			pageQuery.setCurrentPage(Integer.parseInt(page));
			pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
			pageQuery.setShowCount(Integer.parseInt(rows));

			List<MailHistory> mailHistories = mailHistoryService.getMailHistoryPage(info, pageQuery);
			map.put("totalCnt",  pageQuery.getTotalResult());
			map.put("totalPage", pageQuery.getTotalPage());
			map.put("mailHistoryList",mailHistories);
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备类型查询结束");
			actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "邮件日志查询");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "设备基础信息查询异常" + e.getMessage());
		}
		return map;
	}
}
